define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form,Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'org/orgcate/index' + location.search,
                    add_url: 'org/orgcate/add',
                    edit_url: 'org/orgcate/edit',
                    del_url: 'org/orgcate/del',
                    gradeadd_url: 'org/orgcate/gradeadd',
                    gradeedit_url: 'org/orgcate/gradeedit',
                    gradedel_url: 'org/orgcate/gradedel',
                    multi_url: 'org/orgcate/multi',
                    table: 'org_cate',
                }
            });

            var table = $("#table");
             table.on('load-success.bs.table', function (e, data) {
                 //这里可以获取从服务端获取的JSON数据
                 //这里我们手动设置底部的值
                  $("#current_img").text(data.current.avatar);
                  $("#current_phone").text(data.current.mobile);
                 $("#current_name").text(data.current.name);
                 $("#current_time").text(data.current.createtime);
                 $("#username").text(data.current.username);
             });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                escape: false,
                columns: [
                    [
                        //{checkbox: true},
                        {
                            field: 'id',
                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                            operate: false,
                            formatter: Controller.api.formatter.subnode
                        },
                        {field: 'id', title: __('单位编号'), operate:false},
                        {field: 'name', title: __('Name'),  operate:'LIKE'},
                        // {field: 'parent_id', title: __('Parent_id'), operate:false},
                        // {field: 'level', title: __('Level'), operate:false},
                        // {field: 'reg_user_num', title: __('Reg_user_num'), operate:false},
                        // {field: 'visitor_user_num', title: __('Visitor_user_num'), operate:false},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'introduce', title: __('Introduce'), operate:false},
                       /* {field: 'status', title: __('Status'), operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},*/
                        // {field: 'mobile', title: __('Mobile'), operate:'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,align: 'right',
                            // formatter: Table.api.formatter.operate,
                            formatter: Controller.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add',
                                    title: __('添加下级单位'),
                                    text:'添加下级单位',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-plus',
                                    url: 'org/orgcate/add',
                                    extend: 'data-area=["100%","100%"]',
                                    callback: function (data) {
                                        console.log('data',data);
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'grade',
                                    title: __('单位属性设置'),
                                    text: __('单位属性设置'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs btn-ajax',
                                    //icon: 'fa fa-folder-o',
                                    url: 'org/orgcate/grade'+ location.search
                                    // url: 'learn/chapter/index'+ location.search
                                }
                            ]

                        }
                    ]
                ],
                pagination: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:false,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                //默认展示所有子节点
                //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                $(".btn-node-sub.disabled").closest("tr").show();
                $(".btn-node-sub.btn-hidden").closest("tr").show();

                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    //var status = $(this).data("shown") ? false : true;
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function (e) {
                        $(this).closest("tr").toggle(status);
                    });
                    if(status){
                        $(this).children("i").removeClass('fa-plus').addClass('fa-minus');
                    }else{
                        $(this).children("i").removeClass('fa-minus').addClass('fa-plus');

                    }

                    $(this).data("shown", !status);
                    return false;
                });
                //点击切换/排序/删除操作后刷新左侧菜单
                $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
                    Fast.api.refreshmenu();
                    return false;
                });

            });
            //批量删除后的回调
            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Fast.api.refreshmenu();
            });
            //展开隐藏一级
            // $(document.body).on("click", ".btn-toggle", function (e) {
            //     $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
            //     var that = this;
            //     var show = $("i", that).hasClass("fa-chevron-down");
            //     $("i", that).toggleClass("fa-chevron-down", !show);
            //     $("i", that).toggleClass("fa-chevron-up", show);
            //     $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
            //     $(".btn-node-sub[data-pid=0]").data("shown", show);
            // });
            //展开隐藏全部
            //$(document.body).on("click", ".btn-toggle-all", function (e) {
            //    var that = this;
            //    var show = $("i", that).hasClass("fa-plus");
            //    $("i", that).toggleClass("fa-plus", !show);
            //    $("i", that).toggleClass("fa-minus", show);
            //    $(".btn-node-sub.disabled").closest("tr").toggle(show);
            //    $(".btn-node-sub").data("shown", show);
            //});
            $(document).on('click','.btn-setting', function (event) {
                var ids = $(this).data('id');
                Fast.api.open('org/orgcate/grade?ids='+ids, '单位属性设置', {
                    area:["100%","100%"],
                    callback:function(){
                        Fast.api.close();
                        $(".btn-refresh").trigger('click');//刷新当前页面的数据
                    }
                })
            });

            $(document).on('click','.btn-setting2', function (event) {
                var ids = $(this).data('id');
                Fast.api.open('org/orgcate/grade2?ids='+ids, '添加学校班号', {
                    area:["100%","100%"],
                    callback:function(){
                        Fast.api.close();
                        $(".btn-refresh").trigger('click');//刷新当前页面的数据
                    }
                })
            });
            //$(".btn-setting").on('click',function(e){
            //    var ids = $(this).data('id');
            //    Fast.api.open('org/orgcate/grade?ids='+ids, '单位属性设置', {
            //        area:["100%","100%"],
            //        callback:function(){
            //            Fast.api.close();
            //            $(".btn-refresh").trigger('click');//刷新当前页面的数据
            //        }
            //    })
            //})
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        grade: function () {
            Controller.api.bindevent();
        },
        grade2: function () {
            Controller.api.bindevent();
        },
        gradeadd: function () {
            Controller.api.bindevent();
        },
        gradeedit: function () {
            Controller.api.bindevent();
        },
        gradedel: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs btn-success '+ (row.level == 3? 'btn-hidden' : '') + ' btn-node-sub">'+ (row.level == 3? '' : '<i class="fa fa-minus"></i>')+'</a>';
                },
                operate: function (value, row, index) {
                    return '<a href="/admin/org/orgcate/add/ids/' + row.id + '" class="btn btn-xs btn-success btn-dialog '+ (row.level == 3? 'hide' : '')+'" data-area="[&quot;100%&quot;,&quot;100%&quot;]" title="添加下级单位" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="0"><i class="fa fa-plus"></i> 添加下级单位</a>&nbsp;&nbsp;' +
                        '<a href="javascript:;" class="btn btn-xs btn-info btn-setting" data-id="' + row.id + '" title="设置单位属性" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="1" data-area=\'["100%","100%"]\'><i class="fa fa-folder-o"></i> 设置单位属性</a>&nbsp;&nbsp;' +
                        '<a href="javascript:;" class="btn btn-xs btn-info btn-setting2" data-id="' + row.id + '" title="添加学校班号" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="3" data-area=\'["100%","100%"]\'><i class="fa fa-folder-o"></i> 添加学校班号</a>&nbsp;&nbsp;' +
                        '<a href="/admin/org/orgcate/edit/ids/' + row.id + '" class="btn btn-xs btn-success btn-editone '+ (row.level == 1? 'disabled' : '')+'" data-toggle="tooltip" title="" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="4" data-original-title="编辑"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;' +
                        '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone '+ (row.level == 1? 'disabled' : '')+'" data-toggle="tooltip" title="删除" data-table-id="table" data-field-index="10" data-row-index="' +index + '" data-button-index="5"><i class="fa fa-trash"></i></a>';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});